/**
 * Created by CHENCO7 on 8/17/2017.
 */
function merchantFoodList(templateOfCategories, templateOfFoods) {
    var loadFlag = 1;
    var self = this;
    var m_id;
    this.modelOfCategories = null;
    this.modelOfFoods = null;
//        this.templateOfCategories = templateOfCategories;
//        this.templateOfFoods = templateOfFoods;
    /*获取菜品种类*/
    this.findAllCategories = function () {
        j.ajax("GET", "categories", null, null, this, function (categories) {
            this.modelOfCategories = categories;
            _render_Categories(this.modelOfCategories);
        });
    };
    /*根据菜品种类和商家id获取商家爱列表*/
    this.findFoods = function (typeId, m_id) {
        j.ajax("GET", "foods?typeId=" + typeId + "&m_id=" + $("#merchant-id-holder").attr("value"), null, null, this, function (foods) {
            this.modelOfFoods = foods;
            _render_Foods(this.modelOfFoods);
        });
    };


    function _render_Categories(data) {
        console.log(data);
        templateOfCategories.empty();
        $("#category-list").append($("<li value='all' class='category-type-per'  role='presentation'></li>").addClass("active").append($("<a href='javascript:void(0);'></a>").text("All")));
        data.forEach(function (item) {
            $("#category-list").append($("<li value='" + item.id + "' class='category-type-per' role='presentation'></li>").append($("<a href='javascript:void(0);'></a>").text(item.type)));
        });
        $("li.category-type-per").click(function () {
            var m_id = $(this).attr("value");
            $(this).siblings().filter(".active").removeClass("active");
            $(this).addClass("active");
            self.findFoods($(this).attr("value"), m_id);
        });
    }

    function _render_Foods(data) {
        templateOfFoods.empty();
        console.log(data);
        data.forEach(function (item) {
            var clone = $("#food-model").clone().removeClass("hidden-food-pattern");
            clone.children("div:nth-child(1)").children("div:nth-child(1)").children("img").attr("src", imgUrlPrefix + item.image).attr("title", item.image);
            clone.children("div:nth-child(1)").children("div:nth-child(2)").children("h3").text(item.name);
            clone.children("div:nth-child(1)").children("div:nth-child(2)").children("p").text(item.price);
            clone.children("div:nth-child(1)").children("div:nth-child(2)").children("a:first").attr("value", item.id);
            clone.children("div:nth-child(1)").children("div:nth-child(2)")
                .children("a:first")
                .on("click", function () {
                    $("#image-modal").attr("src", imgUrlPrefix + item.image).attr("title", item.image);
                    $("input[name=id]").val(item.id);
                    $("input[name=name]").val(item.name);
                    $("input[name=price]").val(item.price);
                });
            /*添加food操作*/
            clone.children("div:nth-child(1)")
                .children("div:nth-child(2)")
                .children("a:last").on("click", function () {
            });
            templateOfFoods.append(clone);
        });

        if(loadFlag > 0){
            //选择广告
            var foodId = getUrlParameter("foodId");
            if(foodId){
                $("a[value=" + foodId + "]").click();
            }
            loadFlag = 0;
        }
    }
}